const fs = require('fs')
const baseUrl = 'http://10.242.31.16:8999/hr/'
const baseUrl2 = 'http://10.242.31.16:8999/hr/'

const myFetch = (baseUrl, apiInfo, env) => {
    const body = { ...apiInfo.body }
    Object.keys(body).forEach(key => {
        body[key] = env[key] || body[key]
    })
    return fetch(baseUrl + apiInfo.url, {
        "headers": {
            "accept": "application/json, text/javascript, */*; q=0.01",
            "accept-language": "zh-CN,zh;q=0.9",
            "content-type": "application/json;charset=UTF-8",
            "x-access-token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE2NzY3NzAxMDksInVzZXJuYW1lIjoid3V4eTE2MyJ9._ppUo5l80RJEVUcnblf7ZQ3qlyY28w13y0gAUzhpUC8",
            "x-requested-with": "XMLHttpRequest"
        },
        "referrer": "http://10.242.31.16:8999/hrMonthPerspective/Index5",
        "referrerPolicy": "strict-origin-when-cross-origin",
        "body": JSON.stringify(body),
        "method": "POST",
        "mode": "cors",
        "credentials": "include"
    }).then(res => res.text())
}

const apisInfo = [
    {
        "url": "monthlyPerspective/getMatchingTrend",
        "body": {
            "acctDate": "202208",
            "orgId": "OG0619",
            "gw": "204",
            "profLine": "5",
            "cities": [
                "188",
                "720"
            ]
        }
    },
    {
        "url": "monthlyPerspective/getUnemployedReason",
        "body": {
            "acctDate": "202208",
            "orgId": "OG0619",
            "gw": "204",
            "profLine": "5",
            "cities": [
                "188",
                "720"
            ]
        }
    },
    {
        "url": "monthlyPerspective/getInserviceAnalysis",
        "body": {
            "acctDate": "202208",
            "orgId": "OG0619",
            "gw": "204",
            "profLine": "5",
            "cities": [
                "188",
                "720"
            ]
        }
    },
    {
        "url": "monthlyPerspective/getActivityTrend",
        "body": {
            "acctDate": "202208",
            "orgId": "OG0619",
            "gw": "204",
            "profLine": "5",
            "cities": [
                "188",
                "720"
            ]
        }
    },
    {
        "url": "monthlyPerspective/getPostComposition",
        "body": {
            "acctDate": "202208",
            "orgId": "OG0619",
            "gw": "204",
            "profLine": "5",
            "cities": [
                "188",
                "720"
            ]
        }
    },
    {
        "url": "monthlyPerspective/getAverageAge",
        "body": {
            "acctDate": "202208",
            "orgId": "OG0619",
            "gw": "204",
            "profLine": "5",
            "cities": [
                "188",
                "720"
            ]
        }
    },
    {
        "url": "monthlyPerspective/getAgeDistribution",
        "body": {
            "acctDate": "202208",
            "orgId": "OG0619",
            "gw": "204",
            "profLine": "5",
            "cities": [
                "188",
                "720"
            ]
        }
    },
    {
        "url": "monthlyPerspective/getEducationalComposition",
        "body": {
            "acctDate": "202208",
            "orgId": "OG0619",
            "gw": "204",
            "profLine": "5",
            "cities": [
                "188",
                "720"
            ]
        }
    },
    {
        "url": "monthlyPerspective/getUnemployedTrend",
        "body": {
            "acctDate": "202208",
            "orgId": "OG0619",
            "gw": "204",
            "profLine": "5",
            "cities": [
                "188",
                "720"
            ]
        }
    },
    {
        "url": "monthlyPerspective/getReasonAnalysis",
        "body": {
            "acctDate": "202208",
            "orgId": "OG0619",
            "gw": "204",
            "profLine": "5",
            "cities": [
                "188",
                "720"
            ]
        }
    },
    {
        "url": "monthlyPerspective/getKpiTrend",
        "body": {
            "acctDate": "202208",
            "orgId": "OG0619",
            "gw": "204",
            "profLine": "5",
            "cities": [
                "188",
                "720"
            ]
        }
    },
    {
        "url": "monthlyPerspective/getEfficiencyRank",
        "body": {
            "acctDate": "202208",
            "orgId": "OG0619",
            "gw": "204",
            "profLine": "5",
            "page": "1",
            "order": "RANK1",
            "sort": "ASC"
        }
    },
    {
        "url": "monthlyPerspective/moneyTableData",
        "body": {
            "acctDate": "202208",
            "orgId": "OG0619",
            "gw": "204",
            "profLine": "5",
            "cities": [
                "188",
                "720"
            ]
        }
    },
    {
        "url": "monthlyPerspective/getAvgCapacity",
        "body": {
            "acctDate": "202208",
            "orgId": "OG0619",
            "gw": "204",
            "profLine": "5",
            "cities": [
                "188",
                "720"
            ]
        }
    },
    {
        "url": "monthlyPerspective/getWLXWorkDetail",
        "body": {
            "acctDate": "202208",
            "orgId": "OG0619",
            "gw": "204",
            "profLine": "5"
        }
    },
    {
        "url": "monthlyPerspective/getORGNumByKind",
        "body": {
            "acctDate": "202208",
            "orgId": "OG0619",
            "gw": "204",
            "profLine": "5",
            "gwKind": "601"
        }
    },
    // {
    //     "url": "monthlyPerspective/getWorkOrderDetail",
    //     "body": {
    //         "acctDate": "202208",
    //         "orgId": "OG0619",
    //         "gw": "204",
    //         "profLine": "5",
    //         "gwKind": "601",
    //         "page": 1,
    //         "size": 10,
    //         "operation_type": 1
    //     }
    // }
]
const org = [
    {
        "id": "000",
        "text": "广东省分公司"
    },
    {
        "id": "OG0614",
        "text": "广州"
    },
    {
        "id": "OG0619",
        "text": "深圳"
    },
    {
        "id": "OG0111",
        "text": "东莞"
    },
    {
        "id": "OG0618",
        "text": "佛山"
    },
    {
        "id": "OG0611",
        "text": "惠州"
    },
    {
        "id": "OG0113",
        "text": "珠海"
    },
    {
        "id": "OG0114",
        "text": "中山"
    },
    {
        "id": "OG0112",
        "text": "江门"
    },
    {
        "id": "OG0615",
        "text": "汕头"
    },
    {
        "id": "OG0607",
        "text": "湛江"
    },
    {
        "id": "OG0609",
        "text": "揭阳"
    },
    {
        "id": "OG0608",
        "text": "肇庆"
    },
    {
        "id": "OG0616",
        "text": "清远"
    },
    {
        "id": "OG0603",
        "text": "韶关"
    },
    {
        "id": "OG0605",
        "text": "潮州"
    },
    {
        "id": "OG0613",
        "text": "茂名"
    },
    {
        "id": "OG0617",
        "text": "河源"
    },
    {
        "id": "OG0612",
        "text": "汕尾"
    },
    {
        "id": "OG0606",
        "text": "阳江"
    },
    {
        "id": "OG0007",
        "text": "梅州"
    },
    {
        "id": "OG0604",
        "text": "云浮"
    },
    {
        "id": "OG0610",
        "text": "省本部"
    }
]

// [202201 - 202301]
const data =
    Array(12).fill(1).map((_, idx) => (++idx).toString().padStart(2, '0')).map(it => `2022${it}`).concat('202301')
const testdata={
    a: 1,
    b: [{ a: 1 }]
}
    fs.writeFileSync('./test.csv', `${JSON.stringify(testdata)},`+JSON.stringify(testdata))

const main = async () => {
    const acctDate = data[1]
    const tasks = []
    const tags = []
    // org.forEach(it => {
    const it = org[2]
    apisInfo.forEach(info => {
        tasks.push(Promise.allSettled([myFetch(baseUrl, info, { acctDate, orgId: it.id }), myFetch(baseUrl2, info, { acctDate, orgId: it.id })]))
        tags.push(`${acctDate}-${it.text}-${info.url}`)
    })
    // })
    const res = await Promise.allSettled(tasks)
    const log = res.map((it, idx) => {
        return { res: it.value[0].value === it.value[1].value, data: it.value, tag: tags[idx] }
    })

    console.log(log)

}
main()